package com.youbar.client.interceptor;

import com.youbar.client.basic.BizResult;
import com.youbar.client.enums.ResultCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 自定义异常处理
 */
@RestControllerAdvice
public class InterfaceHandler {

    /**
     * 拦截所有运行时的全局异常   
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public BizResult runtimeException(RuntimeException e) {
//        log.error(e.getMessage(), e);
        // 返回 JOSN
        return BizResult.error(ResultCode.ERROR);
    }

    /**
     * 系统异常捕获处理
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BizResult exception(Exception e) {
//        log.error(e.getMessage(), e);
        if (e.getMessage().equals(ResultCode.USER_TOKEN_UNUSUAL.getMessage())) {
            // 登录异常校验
            return BizResult.error(ResultCode.USER_TOKEN_UNUSUAL);
        }
        return BizResult.error(ResultCode.ERROR);
    }

}
